1. Field of the Invention
The present invention relates generally to computer programming languages and more specifically to a mechanism implemented in the Java programming language for handling threads in a manner which avoids leaving the threads in unstable states when they are stopped.
2. Description of Related Art
Java is a high-level programming language which supports multi-threaded program execution. A thread is basically a separate stream of execution that can take place independently from and concurrently with other streams of execution. A thread is similar to a small program that performs particular tasks within a larger program. If, for some reason, there is a problem with the execution of the tasks performed by a particular thread, other threads may continue performing their own tasks despite this problem. For example, if one thread becomes stuck in an infinite loop, the other threads may continue and complete their processing without having to wait for the first thread to break out of the loop.
While threading is a similar to multitasking, it is typically more difficult to implement. This is due, in part, to the fact that multitasking involves individual programs that are executed in isolation from each other, while threading involves the performance of tasks which may be interrelated. For example, one thread may print the contents of certain memory locations, while another thread may write new data to those same memory locations. If the interaction between the threads is not handled properly, the results of the tasks performed by the two threads may be uncertain.
Threads can be implemented in Java by creating a subclass of java.lang.Thread, or by using the java.lang.Runnable interface. The java.lang.Thread class includes three methods which are the primary means for controlling threads: start( ), run( ) and stop( ). The start( ) method prepares the thread to be executed. The run( ) method performs the functions of the thread. The stop( ) method terminates the thread. The java.lang.Thread class also includes several other methods which are used to control the execution of instructions in a thread, including suspends, resumes, sleep( ) and yield( ).
Two of these methods are inherently problematic. It may be unsafe to use the stop( ) method because, when a thread is terminated, objects which may have been locked by the thread are all unlocked, regardless of whether or not they were in consistent states. These inconsistencies can lead to arbitrary behavior which make corrupt the functions of the thread and the program. The suspend( ) method may also cause problems because it is prone to deadlock. For example, if a thread holds a lock on the monitor for a particular resource when it is suspended, no other thread can access the resource until the thread holding the lock is resumed. If a second thread should cause the first thread to resume, but first attempts to access the resource, it may wait for access to the resource and consequently may never call the first thread""s resume( ) method. Because these two methods are so problematic, their use is discouraged and they are being deprecated from the Java Developers Kit produced by Java""s originator, Sun Microsystems Inc.
One or more of the problems outlined above may be solved by various embodiments of the present mechanism. The mechanism provides a means for controlling threads in a Java application while avoiding the unsafe conditions inherent in the use of existing java.lang.Thread methods. The present mechanism provides a simple and easy-to-use mechanism. for stopping threads without causing unnecessary waiting, without creating a need for exception handling, and without leaving the associated application in an unknown state.
In one embodiment, a class is defined for handling threads in an application. The class uses a target variable to indicate whether a thread should continue to run, or whether it should be stopped. This class provides a start( ) method to set up the target variable, a stop( ) method to set the target variable to indicate that the thread should be stopped, and an abstract run( ) method. An abstract method is one which is defined, but contains no functionalityxe2x80x94the functionality of the method must be provided by subclasses which extend this class. At least one subclass is created to extend the class. The subclass overrides the abstract run( ) method and defines the tasks to be performed by threaded objects instantiated from this class.
When an object is instantiated from the subclass, the start( ) method inherited from the class is configured to create a thread having the object as its target. The start( ) method is also configured to set the target variable (which is local to the thread) is set to a value which indicates that the thread should be running. The stop( ) method of the class is also inherited by the subclass. When the stop( ) method is invoked, it is configured to set the target variable to a value which indicates that the thread should be stopped. The run( ) method provided by the subclass periodically checks the target variable within the thread. The checking of the target variable occurs in the normal course of execution of the run method. If the target variable indicates that the thread should be stopped, the run( ) method is configured to complete execution and exit normally, causing the thread to terminate. An exception is not required to stop the run( ) method, so exception handling is not necessary.
In one embodiment, a computer readable storage medium contains instructions defining the class and subclasses described above.
In one embodiment, a thread-handling method is provided for improved handling of threads in Java applications. Broadly speaking, the method comprises providing a class that includes methods for stopping threads based on the indication of a target variable. Instructions that are to be executed within threads are provided in the run( ) methods of subclasses that extend the first class. Rather than creating threads from the standard Thread class and individually configuring the threads to stop execution upon the occurrence of a particular condition, threads are created using the subclasses above. The safer methods which are inherited from these subclasses override the methods of the Thread class so that stopping threads is inherently safer. When a thread is created, the target variable is initialized to indicate that the thread should be running. The instructions being executed by the thread periodically check the target variable to determine whether it indicates that the thread should continue running, or should stop. If the target variable indicates that the thread should continue running, the thread executes normally until the target variable is checked again. If the target variable indicates that the thread should be stopped, the run( ) method completes execution and exits normally.